package com.baizhi.controller;

import com.baizhi.entity.News;
import com.baizhi.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class NewsController {
    @Autowired
    private NewsService ns;
    @Value("${imgpath}")
    private String filePath;
    @GetMapping("/news")
    public List<News> selectAll(){
        List<News> news = ns.selectAll();
        return news;
    }
    @PostMapping("/upload")
    public Map<String,Object> upload(MultipartFile mf) throws IOException {
        System.out.println(filePath);
        String fileName = UUID.randomUUID().toString()+mf.getOriginalFilename();
        System.out.println(fileName);
        mf.transferTo(new File(filePath,fileName));
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("fileName",fileName);
        //return new HashMap(){{put("code",1);put("fileName","test.bmp");}};
        return map;
    }
}
